package com.impress;

import com.impress.listener.OnScrollListener;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;

public class ItemWebView extends WebView {

	public ItemWebView(Context context) {
		super(context);
	}

	public ItemWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public ItemWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	private boolean scrollingVertically;
	private OnScrollListener mScrollListener;
	
	public void setOnScrollListener(OnScrollListener listener){
		this.mScrollListener = listener;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		super.onInterceptTouchEvent(ev);
		return scrollingVertically;
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			scrollingVertically = false;
			break;

		default:
			break;
		}
		return super.onTouchEvent(ev);
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
//		View view = (View)getChildAt(getChildCount()-1);
		float diff = this.getContentHeight()*this.getScale()-(this.getBottom()+getScrollY());
//		int diff = (view.getBottom()-(view.getHeight()+getScrollY()));
		if(diff <= 10){
			if(mScrollListener!=null){
				mScrollListener.ScrollToBottom();
			}
		}else{
			if(mScrollListener!=null){
				mScrollListener.Scrolling();
			}
		}
		super.onScrollChanged(0, t, oldl, oldt);
	}

	@Override
	protected void onFocusChanged(boolean focused, int direction,
			Rect previouslyFocusedRect) {
		if(focused){
//			this.clearFocus();
		}
		super.onFocusChanged(focused, direction, previouslyFocusedRect);
	}
	
	
	

}
